package br.com.foxinline.manager.criar;

import br.com.foxinline.manager.generico.ManagerPesquisarGenerico;
import br.com.foxinline.modelo.*;
import br.com.foxinline.servico.*;
import br.com.foxinline.util.Msg;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author laverson
 */
@ViewScoped
@ManagedBean
public class ManagerLogradouro extends ManagerPesquisarGenerico<Dne> {

    @EJB
    DneServico dneServico;
    private Dne dne;
    private Dne pesquisarDne;
    private Dne removerDne;
    private String dneId;

    @Override
    @PostConstruct
    public void init() {
        setServico(dneServico);

        this.dne = new Dne();
        this.dne.setBairro(new Bairro());
        this.pesquisarDne = new Dne();

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.dneId = params.get("logradouro");
        paramPost();
    }

    public void salvar() {
        try {

            if (this.dne.getId() == null) {
                dneServico.save(this.dne);
                init();
                Msg.messagemInfo(Msg.SuccessFull);

            } else {

                dneServico.update(this.dne);
                this.dne = dneServico.find(this.dne.getId());
                Msg.messagemInfo(Msg.SuccessFull);
            }

        } catch (Exception e) {

            System.err.println(e);
            Msg.messagemError(Msg.Failure);
        }
    }

    @Override
    public void remover() {
        try {
            this.dne.setAtivo(Boolean.FALSE);
            dneServico.update(this.dne);
            Msg.messagemInfo(Msg.SuccessFull);

        } catch (Exception e) {
            System.err.println(e);
            Msg.messagemError(Msg.Failure);
        }
    }

    private void paramPost() {

        if (this.dneId != null) {
            this.dne = dneServico.find(Long.parseLong(this.dneId));
        }
    }

    @Override
    public void limpar() {
        init();
    }

    @Override
    public void pesquisar() {

        this.entidadePesquisar = this.pesquisarDne;

        getSearchResults().setFirst(0);
        onLoadModel();
    }

    public Dne getDne() {
        return dne;
    }

    public void setDne(Dne dne) {
        this.dne = dne;
    }

    public Dne getPesquisarDne() {
        return pesquisarDne;
    }

    public void setPesquisarDne(Dne pesquisarDne) {
        this.pesquisarDne = pesquisarDne;
    }

    public Dne getRemoverDne() {
        return removerDne;
    }

    public void setRemoverDne(Dne removerDne) {
        this.removerDne = removerDne;
    }
}
